from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from module_stock.entity.do.info_do import StockInfo
from module_stock.entity.do.trade_do import Trade
from module_stock.entity.vo.trade_vo import TradeModel, TradePageQueryModel
from utils.page_util import PageUtil


class TradeDao:
    """
    股票交易任务模块数据库操作层
    """

    @classmethod
    async def get_trade_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据主键ID获取股票交易任务详细信息

        :param db: orm对象
        :param id: 主键ID
        :return: 股票交易任务信息对象
        """
        trade_info = (
            (
                await db.execute(
                    select(Trade)
                    .where(
                        Trade.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return trade_info

    @classmethod
    async def get_trade_detail_by_info(cls, db: AsyncSession, trade: TradeModel):
        """
        根据股票交易任务参数获取股票交易任务信息

        :param db: orm对象
        :param trade: 股票交易任务参数对象
        :return: 股票交易任务信息对象
        """
        trade_info = (
            (
                await db.execute(
                    select(Trade).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return trade_info

    @classmethod
    async def get_trade_list(cls, db: AsyncSession, query_object: TradePageQueryModel, is_page: bool = False):
        """
        根据查询参数获取股票交易任务列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 股票交易任务列表信息对象
        """
        query = (
            select(Trade, StockInfo.stock_name)
            .join(StockInfo, Trade.stock_code == StockInfo.stock_code, isouter=True)
            .where(
                Trade.stock_code == query_object.stock_code if query_object.stock_code else True,
                Trade.direction == query_object.direction if query_object.direction else True,
                Trade.source == query_object.source if query_object.source else True,
                Trade.account_no == query_object.account_no if query_object.account_no else True,
                Trade.phase == query_object.phase if query_object.phase else True,
                Trade.status == query_object.status if query_object.status else True,
                Trade.status != '0',
            )
            .order_by(Trade.update_time.desc())
            .distinct()
        )
        trade_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return trade_list

    @classmethod
    async def add_trade_dao(cls, db: AsyncSession, trade: TradeModel):
        """
        新增股票交易任务数据库操作

        :param db: orm对象
        :param trade: 股票交易任务对象
        :return:
        """
        db_trade = Trade(**trade.model_dump(exclude={'id'}))
        db.add(db_trade)
        await db.flush()

        return db_trade

    @classmethod
    async def edit_trade_dao(cls, db: AsyncSession, trade: dict):
        """
        编辑股票交易任务数据库操作

        :param db: orm对象
        :param trade: 需要更新的股票交易任务字典
        :return:
        """
        await db.execute(update(Trade), [trade])

    @classmethod
    async def delete_trade_dao(cls, db: AsyncSession, trade: TradeModel):
        """
        删除股票交易任务数据库操作

        :param db: orm对象
        :param trade: 股票交易任务对象
        :return:
        """
        await db.execute(delete(Trade).where(Trade.id.in_([trade.id])))
